from django.db import models


# Create your models here.
# 设计表名和表字段时，表名用全写，字段名用简写，尽量不要让表名和字段名一样，方便理解ORM跨表查询以及双下线查询。
# 如何为表和表字段添加注释？？？
# verbose_name 属性是干嘛的？
class Book(models.Model):
    nid = models.AutoField(primary_key=True)
    title = models.CharField(max_length=32, verbose_name='书籍名称', )
    price = models.DecimalField(max_digits=8, decimal_places=2, verbose_name='书籍价格')  # 最大值: 999999.99
    pub_date = models.DateTimeField('date published')  # django 要求必须存这种格式：2019-07-12
    # 与Publish建立一对多的关系，外键字段建立在多的一方
    publisher = models.ForeignKey(to="Publish", to_field='pid', on_delete=models.CASCADE)
    # 与Author表建立多对多的关系，ManyToManyField可以建在两个模型中的任意一个，自动创建第三张表
    authors = models.ManyToManyField(to="Author")

    def __str__(self):
        return self.title


class Publish(models.Model):
    pid = models.AutoField(primary_key=True)
    publish_name = models.CharField(max_length=32)
    pub_tel = models.IntegerField()
    email = models.CharField(max_length=32)
    pub_address = models.CharField(max_length=32)

    def __str__(self):
        return self.publish_name


class Author(models.Model):
    aid = models.AutoField(primary_key=True)
    name = models.CharField(max_length=32)
    # 删除关联数据,与之关联也删除（这里怎么和外键不一样，外键关联的可以级联删除，一对一关联的级联删除需要手动处理）
    # 与AuthorDetail创建一对一的关系
    author_detail = models.OneToOneField(to="AuthorDetail", on_delete=models.CASCADE)

    def __str__(self):
        return self.name


class AuthorDetail(models.Model):
    age = models.IntegerField()
    email = models.EmailField()
    tel = models.CharField(max_length=32)
    address = models.CharField(max_length=32)
